/**
 *====================================================
 * 文件名称: LoanNoticeServiceImpl.java
 * 修订记录：
 * No    日期				作者(操作:具体内容)
 * 1.    2015年09月23日	chenxy(创建:创建文件)
 *====================================================
 * 类描述：(说明未实现或其它不应生成javadoc的内容)
 */
package com.yuexunit.finance.project.service;

import java.math.BigDecimal;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;

import com.yuexunit.finance.common.util.BeanUtils;
import com.yuexunit.finance.common.util.DateUtils;
import com.yuexunit.finance.common.util.RandomUtil;
import com.yuexunit.finance.project.SettingConstants;
import com.yuexunit.finance.project.entity.FinanceApplicationEntity;
import com.yuexunit.finance.project.entity.LoanNoticeEntity;
import com.yuexunit.finance.project.entity.lexicon.ApplicantLoanStateEnum;
import com.yuexunit.inject.annotation.Implements;
import com.yuexunit.page.PageCriteria;
import com.yuexunit.page.PageList;
import com.yuexunit.persist.PersistUtil;
import com.yuexunit.persist.annotation.Transactional;
import com.yuexunit.util.DateUtil;
import com.yuexunit.finance.project.entity.FinanceBulkstockEntity;

/**
 * @ClassName: LoanNoticeServiceImpl
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @author chenxy
 * @date 2015年09月23日 18:56:17
 */
@Implements(LoanNoticeService.class)
public class LoanNoticeServiceImpl implements LoanNoticeService {

	@Override
	public Long createLoanNotice(LoanNoticeEntity entity) {
		entity.setLoanState(ApplicantLoanStateEnum.WAIT_AUDIT);
		entity.setEnabled(Boolean.TRUE);
		entity.setCreateDateTime(DateUtil.now());
		entity.setUpdateDateTime(DateUtil.now());
		entity.setLoanNoticeNumber(SettingConstants.PRE_NUMBER_LOAN_NOTICE + RandomUtil.generateTimeNumber());
		entity.setLeader2AuditState(Boolean.TRUE);
		entity.setFinancialLeaderAuditState(Boolean.TRUE);
		entity.setFinancialAuditState(Boolean.TRUE);
		entity.setAuditState2(Boolean.TRUE);
		entity.setAuditState1(Boolean.TRUE);
		entity.setLeader1AuditState(Boolean.TRUE);
		PersistUtil.insert(entity);
		return entity.getLoanNoticeId();
	}

	@Override
	public void updateLoanNotice(LoanNoticeEntity entity) {
		LoanNoticeEntity persist = inquireLoanNoticeEntity(entity.getLoanNoticeId());
		BeanUtils.copyProperties(entity, persist);
		persist.setUpdateDateTime(DateUtil.now());
		PersistUtil.update(persist);
	}

	@Override
	public void renovateLoanNotice(LoanNoticeEntity persist) {
		PersistUtil.update(persist);
	}

	@Override
	public LoanNoticeEntity inquireLoanNoticeEntity(Long loanNoticeId) {
		if (loanNoticeId == null) {
			return null;
		}
		return PersistUtil.get(LoanNoticeEntity.class, loanNoticeId);
	}

	@Override
	public PageList<LoanNoticeEntity> inquireLoanNoticeList(String beginDate, String endDate, Long financeId, String financeNumber, Long enterpriseId, String enterpriseName,String bulkstockName, ApplicantLoanStateEnum loanState,List<Long> financeIds, PageCriteria page) {
		List<Long> financeIds2 = null;
		if (bulkstockName !=null && !"".equals(bulkstockName)) {// 若有搜索货物名称
			DetachedCriteria criteria = DetachedCriteria.forClass(FinanceBulkstockEntity.class);
			if (StringUtils.isNotBlank(bulkstockName)) {
				criteria.add(Restrictions.like("bulkstockName", "%" + bulkstockName + "%"));
			}
			criteria.setProjection(Projections.projectionList().add(Projections.groupProperty("financeId")));
			financeIds2 = PersistUtil.get(criteria);
			if (financeIds2.isEmpty()) {
				return new PageList<>(page);
			}
		}
		DetachedCriteria criteria = DetachedCriteria.forClass(LoanNoticeEntity.class);
		if (financeId != null) {
			criteria.add(Restrictions.eq("financeId", financeId));
		}
		
		if (loanState != null) {
			criteria.add(Restrictions.eq("loanState", loanState));
		}
		if (financeIds2 != null && financeIds2.size() > 0) {
			criteria.add(Restrictions.in("financeId", financeIds2));
		}
		if (StringUtils.isNotBlank(beginDate)) {
			criteria.add(Restrictions.ge("loanDate", DateUtils.parseDateInt(beginDate)));
		}
		
		if (financeIds == null || financeIds.size() == 0) {
			return new PageList<LoanNoticeEntity>(page);
		}
		criteria.add(Restrictions.in("financeId", financeIds));
		
		if (StringUtils.isNotBlank(endDate)) {
			criteria.add(Restrictions.le("loanDate", DateUtils.parseDateInt(endDate)));
		}

		if (StringUtils.isNotBlank(financeNumber)) {
			criteria.add(Restrictions.like("financeNumber", "%" + financeNumber + "%"));
		}
		
		if (enterpriseId != null) {
			criteria.add(Restrictions.eq("enterpriseId", enterpriseId));
		}
		
		if (StringUtils.isNotBlank(enterpriseName)) {
			criteria.add(Restrictions.like("enterpriseName", "%" + enterpriseName + "%"));
		}
		
		
		criteria.addOrder(Order.desc("createDateTime"));
		 
//		PageList<LoanNoticeEntity> pages =	PersistUtil.get(criteria, page);
				
		return PersistUtil.get(criteria, page);
	}
	
	@Override
	public List<Long> countWaitAuditLoadFinanceApplication() {
		DetachedCriteria criteria = DetachedCriteria.forClass(LoanNoticeEntity.class);
		criteria.add(Restrictions.isNull("auditorId"));
		criteria.setProjection(Projections.projectionList().add(Projections.groupProperty("loanNoticeId")));
		return PersistUtil.get(criteria);
	}

	@Override
	public BigDecimal inquireFinanceUnConsentLoanAmount(FinanceApplicationEntity finance) {
		DetachedCriteria criteria = DetachedCriteria.forClass(LoanNoticeEntity.class);
		criteria.add(Restrictions.eq("enabled", Boolean.TRUE));
		criteria.add(Restrictions.eq("financeId", finance.getFinanceId()));
		criteria.add(Restrictions.eq("loanState", ApplicantLoanStateEnum.WAIT_AUDIT));
		criteria.setProjection(Projections.projectionList().add(Projections.sum("loanAmount")));
		List<Object> list = PersistUtil.get(criteria);
		if (list.isEmpty() || list.get(0) == null) {
			return BigDecimal.ZERO;
		}
		return new BigDecimal(list.get(0).toString());
	}
	
	public List<LoanNoticeEntity> inquireInsuranceList() {
		DetachedCriteria criteria = DetachedCriteria.forClass(LoanNoticeEntity.class);
		criteria.add(Restrictions.isNull("auditorId"));
		return PersistUtil.get(criteria);
	}

	@Override
	public List<LoanNoticeEntity> inquireInsuranceCompleteList() {
		DetachedCriteria criteria = DetachedCriteria.forClass(LoanNoticeEntity.class);
		criteria.add(Restrictions.eq("loanState", ApplicantLoanStateEnum.AUDIT));   //状态为2:已审核
		return PersistUtil.get(criteria);
	}

	@Override
	public List<LoanNoticeEntity> inquireLoanNoticeList(Long financeId) {
		DetachedCriteria criteria = DetachedCriteria.forClass(LoanNoticeEntity.class);
		if (financeId != null) {
			criteria.add(Restrictions.eq("financeId", financeId));
		}
		return PersistUtil.get(criteria);
	}

	@Override
	@Transactional
	public void deleteLoanNotice(Long loanNoticeId) {
		PersistUtil.delete(LoanNoticeEntity.class, loanNoticeId);
	}
}
